package com.cjm.algorithm.leetcode;

import java.util.Arrays;

/**
 * 多数 -》 数组中超过一半的数字
 */
public class Ti0169 {
    public static int m1(int[] nums) {
        int cadidate = nums[0];
        int count = 1;
        for (int i = 0; i < nums.length; i++) {
            if (count == 0) {
                cadidate = nums[i];
                count = 1;
            } else if (nums[i] == cadidate) {
                count ++;
            } else {
                count --;
            }
        }
        return cadidate;
    }

    public static int m2(int[] nums) {
        int cadidate = nums[0];
        int count = 1;
        for (int i = 0; i < nums.length; i++) {
            if (count == 0) {
                cadidate = nums[i];
                count ++;
            } else if (nums[i] == cadidate) {
                count ++;
            } else {
                count --;
            }
        }
        return cadidate;
    }
}
